class BlogPost
#host is not a valid option for Mongo::Connection

#Mongoid by default stores documents in a collection that is the pluralized form of the class name.
# So do not use a name that ends in an 's' because this can cause errors.
  
	include Mongoid::Document
	@@id = 0  
	field :title, :type => String, default: "untitled"
  field :content, :type => String, default: "no content entered"
	field :summary, :type => String
	field :tag1, :type => String
	field :tag2, :type => String
	field :tag3, :type => String
	field :time, :type => Time, default: -> { new? ? Time.now : 10.years.ago }
	field :id, :type =>Integer
	 
#scope :titledPosts, excludes(title: "untitled")
scope :titledPosts, where(title: "titled")
scope :atag, where(tag1: "a")
scope :current, where(:time.lte => Time.now)

def self.search2(search)
	search_condition = "%" + search + "%"
	#BlogPost.all(conditions: { title: "titled" })
	#BlogPost.all_in(title: [search])
	BlogPost.any_of({ title: search }, { content: search }, { tag1: search }, { tag2: search }, { tag3: search })	
#BlogPost.where(title: search_condition)
#find(:all, :conditions => ['title LIKE ? OR content #LIKE ?', search_condition, search_condition])
end

def sortasc
	BlogPost.asc(:title)
# another way: BlogPost.ascending(:first_name) .. similarly there is desc
end

def alsoin
	BlogPost.also_in(tag1: [ "a", "b" ])
end

def getdistinct
# Get the distinct values for tag1. Note that BlogPost.all only gets all entries.
	BlogPost.all.distinct(:tag1)
end

def searchallin
# Match all Blogposts with title = "titled".
@blogPosts = BlogPost.all_in(title: ["titled"])
	end

def searchByTitle
     @blog_posts =BlogPost.where(title: "untitled")
   end
# I used default to set the default value.
# T.c I used curly braces surrounding Time.now to get the time at which creation time. Removing them will get yoy the class load time.

#other valid types are : Array,BigDecimal, boolean, Date, DateTime, Float, Hash, Integer, Range, String, Symbol, Time.

end
